package src.java.medium;

public class Solution189 {
    public static int change(int[] nums, int index, int num, int k) {
        int res = nums[(index + k) % nums.length];
        nums[(index + k) % nums.length] = num;
        return res;
    }

    public static void rotate2(int[] nums, int k) {
        if (nums.length == 1 || k % nums.length == 0) return;
        int length = nums.length;
        int sum = 0;
        int slow = 0;
        int fast = 0;
        k = k % length;
        int num = nums[fast];
        while (sum < length) {
            num = change(nums, fast, num, k);
            fast = (fast + k) % length;
            sum++;
            if (fast == slow) {
                slow = ++fast;
                num = nums[fast];
            }
            ;
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6};
        int k = 2;
        rotate2(nums, k);
    }

    public void rotate(int[] nums, int k) {
        int length = nums.length;
        k = k % length;
        int[] temp = new int[length];
        for (int i = 0; i < length; i++) {
            temp[(i + k) % length] = nums[i];
        }
        for (int i = 0; i < length; i++) {
            nums[i] = temp[i];
        }
    }
}
